/*
 * Copyright (c) 2008-2013 Haulmont. All rights reserved.
 * Use is subject to license terms, see http://www.cuba-platform.com/license for details.
 */

package com.haulmont.asu.web.ui.work;

import com.haulmont.asu.entity.Work;
import com.haulmont.charts.core.entity.GanttChartItem;
import com.haulmont.charts.gui.components.charts.GanttChart;
import com.haulmont.chile.core.datatypes.Datatypes;
import com.haulmont.chile.core.datatypes.FormatStrings;
import com.haulmont.cuba.gui.WindowManager;
import com.haulmont.cuba.gui.components.*;
import com.haulmont.cuba.gui.components.actions.CreateAction;
import com.haulmont.cuba.gui.components.actions.EditAction;
import com.haulmont.cuba.gui.data.CollectionDatasource;
import com.haulmont.cuba.gui.data.Datasource;
import com.haulmont.cuba.gui.data.impl.DsListenerAdapter;
import com.haulmont.cuba.security.global.UserSession;
import com.haulmont.cuba.web.gui.components.WebComponentsHelper;
import com.vaadin.ui.AbstractOrderedLayout;

import javax.inject.Inject;
import javax.inject.Named;
import java.util.Map;
import java.util.UUID;

/**
 * Author: Sergey42
 * Date: 17.10.13 16:31
 */
public class WorkGanttBrowser extends AbstractLookup {

    @Named("splitGantt")
    protected SplitPanel ganttSplit;

    @Named("table-panel")
    protected BoxLayout tableView;

    @Inject
    protected Table worksTable;

    @Inject
    protected CollectionDatasource<Work, UUID> worksDs;

    @Inject
    protected CollectionDatasource<GanttChartItem, UUID> ganttChartDs;

    @Inject
    protected LinkButton showGanttChart;

    @Inject
    protected Button ganttChartRefresh;

    @Inject
    protected GanttChart ganttChart;

    @Inject
    protected UserSession userSession;

    @Inject
    protected BoxLayout mainLayout;

    @Named("split")
    protected SplitPanel tableSplit;

    protected GanttChartLoader ganttLoader;

    @Override
    public void init(Map<String, Object> params) {
        super.init(params);
        initGanttChart();

        worksTable.removeAction(getAction("worksTable.create"));
        worksTable.addAction(new CreateAction(worksTable, WindowManager.OpenType.DIALOG));

        worksTable.removeAction(getAction("worksTable.create"));
        worksTable.addAction(new EditAction(worksTable, WindowManager.OpenType.DIALOG));
    }

    protected void initGanttChart() {
        ganttChart.setShowStartDate(false);
        ganttChart.setShowEndDate(false);
        ganttChart.setShowResource(true);
        ganttChart.setShowInitiator(true);
        FormatStrings formatStrings = Datatypes.getFormatStrings(userSession.getLocale());
        if (formatStrings != null) {
            ganttChart.setDateFormat(formatStrings.getDateFormat());
        }
        /*ganttChart.setTaskClickListener(new GanttChart.TaskClickListener() {
            @Override
            public void performClick(GanttChartItem taskItem) {
                openTask((Entity) taskItem.getRelatedEntity());
            }
        });*/

        ganttLoader = GanttChartLoader.createLoader(ganttChartDs);
        worksDs.addListener(new DsListenerAdapter<Work>() {
            @Override
            public void stateChanged(Datasource<Work> ds, Datasource.State prevState, Datasource.State state) {
                super.stateChanged(ds, prevState, state);
                if ((prevState != state) && (state == Datasource.State.VALID))
                    ganttLoader.loadFromDatasource((CollectionDatasource.Ordered<Work, UUID>) ds);
            }
        });

        final WorkDataSource taskDatasource = (WorkDataSource) worksDs;
        taskDatasource.setSortListener(new WorkDataSource.SortListener() {
            @Override
            public void handleSort(CollectionDatasource.Sortable.SortInfo[] sortInfos) {
                ganttLoader.loadFromDatasource(taskDatasource);
            }
        });


        ganttChartRefresh.setAction(new AbstractAction("actions.Refresh") {
            @Override
            public void actionPerform(Component component) {
                worksDs.refresh();
            }

            @Override
            public boolean isVisible() {
                return ganttChart.isVisible();
            }

            @Override
            public String getCaption() {
                return messages.getMessage(WorkGanttBrowser.class, "refresh");
            }

            @Override
            public String getIcon() {
                return "icons/refresh.png";
            }
        });


        ganttSplit.setLocked(true);
        showGanttChart.setAction(new AbstractAction("") {
            @Override
            public void actionPerform(Component component) {
                tableView.setVisible(ganttChart.isVisible());
                ganttChart.setVisible(!ganttChart.isVisible());

                ganttChartRefresh.setVisible(ganttChart.isVisible());
                if (ganttChart.isVisible()) {
                    showGanttChart.setIcon("icons/edit.png");
                    mainLayout.expand(ganttSplit);
                    com.vaadin.ui.Component layout = WebComponentsHelper.unwrap(mainLayout);
                    if (layout instanceof AbstractOrderedLayout) {
                        ((AbstractOrderedLayout) layout).setExpandRatio(WebComponentsHelper.unwrap(tableSplit), 0.0f);
                    }
                } else {
                    showGanttChart.setIcon("icons/edit.png");
                    mainLayout.expand(tableSplit);
                    com.vaadin.ui.Component layout = WebComponentsHelper.unwrap(mainLayout);
                    if (layout instanceof AbstractOrderedLayout) {
                        ((AbstractOrderedLayout) layout).setExpandRatio(WebComponentsHelper.unwrap(ganttSplit), 0.0f);
                    }
                }
            }
        });
    }
}
